/**
 ****************************************************************************************
 *
 * @file app_proxr_task.c
 *
 * @brief Proximity Reporter application Task Implemantation.
 *
 * Copyright (C) 2012. Dialog Semiconductor Ltd, unpublished work. This computer
 * program includes Confidential, Proprietary Information and is a Trade Secret of
 * Dialog Semiconductor Ltd.  All use, disclosure, and/or reproduction is prohibited
 * unless authorized in writing. All Rights Reserved.
 *
 * <bluetooth.support@diasemi.com> and contributors.
 *
 ****************************************************************************************
 */

/**
 ****************************************************************************************
 * @addtogroup APPTASK
 * @{
 ****************************************************************************************
 */

/*
 * INCLUDE FILES
 ****************************************************************************************
 */

#include "rwble_config.h"              // SW configuration

#if (BLE_APP_PRESENT)

#if (BLE_CUSTOM_SERVER)
#include "app_customs.h"
#include "app_customs_task.h"
#include "custs1_task.h"
#include "custs2_task.h"
#include "app_task.h"           // Application Task API
#include "app_entry_point.h"

/*
 * GLOBAL VARIABLE DEFINITIONS
 ****************************************************************************************
 */
#if (BLE_CUSTOM1_SERVER)
const struct ke_msg_handler app_custs1_process_handlers[]=
{
    {CUSTS1_CREATE_DB_CFM,                 (ke_msg_func_t)app_custs1_create_db_cfm_handler},
    {CUSTS1_DISABLE_IND,                   (ke_msg_func_t)app_custs1_disable_ind_handler},

    {APP_CUSTS1_POWEROFF_CLEAR_COUNT,				(ke_msg_func_t)app_custs1_poweroff_clear_count_handler},
    {APP_CUSTS1_POWERON,  									(ke_msg_func_t)app_custs1_poweron_handler},
			
    {APP_CUSTS1_TIME_UPDATE,  						 (ke_msg_func_t)app_custs1_time_update_handler},
		{APP_CUSTS1_BATT_POLL,  						 	 (ke_msg_func_t)app_custs1_batt_poll_handler},

		{APP_CUSTS1_BATT_LOW_BLINK,								 (ke_msg_func_t)app_custs1_batt_low_blink_handler},
			
		{APP_CUSTS1_ALERT_MOTOR,  						 (ke_msg_func_t)app_custs1_alert_motor_handler},

		{APP_CUSTS1_TIME_OLED_CLOSE,  					 (ke_msg_func_t)app_custs1_oled_close_handler},
    {APP_CUSTS1_TIME_OLED_OPEN, 						(ke_msg_func_t)app_custs1_oled_open_handler},

    {APP_CUSTS1_ALERT_PERIOD, (ke_msg_func_t)app_custs1_alert_period_handler},
};
#endif //BLE_CUSTOM1_SERVER

#if (BLE_CUSTOM2_SERVER)
const struct ke_msg_handler app_custs2_process_handlers[]=
{
    {CUSTS2_CREATE_DB_CFM,                 (ke_msg_func_t)app_custs2_create_db_cfm_handler},
    {CUSTS2_DISABLE_IND,                   (ke_msg_func_t)app_custs2_disable_ind_handler},
		{APP_CUST2_COM_UPDATE,  						 (ke_msg_func_t)app_custs2_com_upate_handler},
};
#endif //BLE_CUSTOM2_SERVER
/*
 * FUNCTION DEFINITIONS
 ****************************************************************************************
 */
#if (BLE_CUSTOM1_SERVER)
enum process_event_response app_custs1_process_handler(ke_msg_id_t const msgid,
                                                       void const *param,
                                                       ke_task_id_t const dest_id,
                                                       ke_task_id_t const src_id, 
                                                       enum ke_msg_status_tag *msg_ret)
{
    return (app_std_process_event(msgid, param, src_id, dest_id, msg_ret, app_custs1_process_handlers,
                                         sizeof(app_custs1_process_handlers)/sizeof(struct ke_msg_handler)));
}

int app_custs1_create_db_cfm_handler(ke_msg_id_t const msgid,
                                      struct custs1_create_db_cfm const *param,
                                      ke_task_id_t const dest_id,
                                      ke_task_id_t const src_id)
{
    if (ke_state_get(dest_id) == APP_DB_INIT)
    {
        // Inform the Application Manager
        struct app_module_init_cmp_evt *cfm = KE_MSG_ALLOC(APP_MODULE_INIT_CMP_EVT,
                                                           TASK_APP, TASK_APP,
                                                           app_module_init_cmp_evt);

        cfm->status = param->status;

        ke_msg_send(cfm);
    }

    return (KE_MSG_CONSUMED);
}

int app_custs1_disable_ind_handler(ke_msg_id_t const msgid,
                                    struct custs1_disable_ind const *param,
                                    ke_task_id_t const dest_id,
                                    ke_task_id_t const src_id)
{
		app_custs1_oled_show_icon_bt(false);
    return (KE_MSG_CONSUMED);
}

int app_custs1_poweroff_clear_count_handler(ke_msg_id_t const msgid,
                                        void const *param,
                                        ke_task_id_t const dest_id,
                                        ke_task_id_t const src_id)
{
		app_cust1_poweroff_clear_count();
    return (KE_MSG_CONSUMED);
}

int app_custs1_poweron_handler(ke_msg_id_t const msgid,
                                        void const *param,
                                        ke_task_id_t const dest_id,
                                        ke_task_id_t const src_id)
{
		app_cust1_poweron_process_judge();
    return (KE_MSG_CONSUMED);
}

int app_custs1_time_update_handler(ke_msg_id_t const msgid,
                                   void const *param,
                                   ke_task_id_t const dest_id,
                                   ke_task_id_t const src_id)
{
    app_custs1_time_update();
		
    return (KE_MSG_CONSUMED);
}

int app_custs1_batt_poll_handler(ke_msg_id_t const msgid,
                                   void const *param,
                                   ke_task_id_t const dest_id,
                                   ke_task_id_t const src_id)
{
    app_custs1_battery_poll();
		
    return (KE_MSG_CONSUMED);
}

int app_custs1_batt_low_blink_handler(ke_msg_id_t const msgid,
                                        void const *param,
                                        ke_task_id_t const dest_id,
                                        ke_task_id_t const src_id)
{
		app_custs1_battery_low_blink();
		
    return (KE_MSG_CONSUMED);
}

int app_custs1_alert_motor_handler(ke_msg_id_t const msgid,
                                   void const *param,
                                   ke_task_id_t const dest_id,
                                   ke_task_id_t const src_id)
{
    app_custs1_alert_motor();

    return (KE_MSG_CONSUMED);
}

int app_custs1_oled_close_handler(ke_msg_id_t const msgid,
                                   void const *param,
                                   ke_task_id_t const dest_id,
                                   ke_task_id_t const src_id)
{
    app_custs1_oled_close();

    return (KE_MSG_CONSUMED);
}

int app_custs1_oled_open_handler(ke_msg_id_t const msgid,
                                   void const *param,
                                   ke_task_id_t const dest_id,
                                   ke_task_id_t const src_id)
{
    app_custs1_set_oled_open();

    return (KE_MSG_CONSUMED);
}


int app_custs1_alert_period_handler(ke_msg_id_t const msgid,
                                        void const *param,
                                        ke_task_id_t const dest_id,
                                        ke_task_id_t const src_id)
{
	 	app_custs1_alert_notify();

    return (KE_MSG_CONSUMED);
}


#endif //BLE_CUSTOM1_SERVER

#if (BLE_CUSTOM2_SERVER)
enum process_event_response app_custs2_process_handler(ke_msg_id_t const msgid,
                                                       void const *param,
                                                       ke_task_id_t const dest_id,
                                                       ke_task_id_t const src_id, 
                                                       enum ke_msg_status_tag *msg_ret)
{
    return (app_std_process_event(msgid, param, src_id, dest_id, msg_ret, app_custs2_process_handlers,
                                         sizeof(app_custs2_process_handlers)/sizeof(struct ke_msg_handler)));
}

int app_custs2_create_db_cfm_handler(ke_msg_id_t const msgid,
                                      struct custs2_create_db_cfm const *param,
                                      ke_task_id_t const dest_id,
                                      ke_task_id_t const src_id)
{
    if (ke_state_get(dest_id) == APP_DB_INIT)
    {
        // Inform the Application Manager
        struct app_module_init_cmp_evt *cfm = KE_MSG_ALLOC(APP_MODULE_INIT_CMP_EVT,
                                                           TASK_APP, TASK_APP,
                                                           app_module_init_cmp_evt);

        cfm->status = param->status;

        ke_msg_send(cfm);
    }

    return (KE_MSG_CONSUMED);
}

int app_custs2_disable_ind_handler(ke_msg_id_t const msgid,
                                    struct custs2_disable_ind const *param,
                                    ke_task_id_t const dest_id,
                                    ke_task_id_t const src_id)
{
    return (KE_MSG_CONSUMED);
}


int app_custs2_com_upate_handler(ke_msg_id_t const msgid,
                                   void const *param,
                                   ke_task_id_t const dest_id,
                                   ke_task_id_t const src_id)
{
    app_custs2_notify_data();
		
		
    return (KE_MSG_CONSUMED);
}


#endif // (BLE_CUSTOM2_SERVER)
#endif // (BLE_CUSTOM_SERVER)

#endif // (BLE_APP_PRESENT)

/// @} APPTASK
